<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" width="40%" @close="closeDialog" @open="openDialog">
    <el-checkbox-group v-model="checkList" v-loading="loading">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button size="small" @click="closeDialog">取消</el-button>
      <el-button type="primary" size="small" @click="addRole">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetails } from '@/api/user'
import { assignRole } from '@/api/permission'
export default {
  name: 'AssignRoles',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      list: [],
      loading: false
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    },
    async openDialog() {
      this.loading = true
      const data = await getRoleList(1, 10000)
      this.list = data.rows
      const result = await getUserDetails(this.userId)
      this.checkList = result.roleIds
      this.loading = false
    },
    async addRole() {
      const data = await assignRole(this.userId, this.checkList)
      console.log(data)
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
